package com.atguigu.gmall.realtime.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author caodan
 * @version 1.0
 * @date 2022-10-08 16:56
 * ik 分词器的使用
 */
public class KeyWordUtil {

    /**
     * 分词
     *
     * @param text 文本
     * @return 分词结果
     */
    public static List<String> analyze(String text) {
        List<String> keywordList = new ArrayList<>();
        StringReader reader = new StringReader(text);
        IKSegmenter ikSegmenter = new IKSegmenter(reader, true);
        Lexeme lexeme = null;
        try {
            while ((lexeme = ikSegmenter.next()) != null) {
                String keyword = lexeme.getLexemeText();
                keywordList.add(keyword);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return keywordList;
    }

}
